package com.lovo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.bean.Tenderinfo2;
import com.lovo.dto.TenderDto;
import com.lovo.dto.TenderInfoDto;
import com.lovo.mapper.Tenderinfo2Mapper;
import com.lovo.service.FSupplierService;
import com.lovo.service.ITenderinfo2Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lovo
 * @since 2022-09-17
 */
@Service
public class Tenderinfo2ServiceImpl extends ServiceImpl<Tenderinfo2Mapper, Tenderinfo2> implements ITenderinfo2Service {

    @Autowired
    FSupplierService fSupplierService;

    @Override
    public IPage<Tenderinfo2> findAllTenderInfo(Integer pageNO) {
        QueryWrapper<Tenderinfo2> qw = new QueryWrapper<>();
        qw.in("i_status", "已中标", "投标中");
        return baseMapper.selectPage(new Page<>(pageNO, 5), qw);
    }

    @Override
    public void updateTenderInfoStatus(Integer tenderInfoId, String status) {
        Tenderinfo2 tenderinfo2 = baseMapper.selectById(tenderInfoId);
        tenderinfo2.setIStatus(status);
        fSupplierService.updateByTenderId(tenderInfoId);
        baseMapper.updateById(tenderinfo2);
    }

    @Override
    public List<Tenderinfo2> findInfoByTenderId(Integer tenderId) {
        QueryWrapper<Tenderinfo2> qw = new QueryWrapper<>();
        qw.eq("fk_tender", tenderId);
        return baseMapper.selectList(qw);
    }


    @RabbitListener(queues = "tenderInfoQueue")
    public void sendTenderInfo(Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag,TenderInfoDto tenderInfoDto) throws IOException {
        Tenderinfo2 tenderinfo2 = new Tenderinfo2();
        BeanUtils.copyProperties(tenderInfoDto, tenderinfo2);
        tenderinfo2.setIStatus("投标中");
        baseMapper.insert(tenderinfo2);
        //手动确认
        channel.basicAck(tag, false);

    }


}
